package com.tianji.coupon.controller;


import com.tianji.common.Result;
import com.tianji.coupon.dto.CouponFormDTO;
import com.tianji.coupon.dto.CouponIssueFormDTO;
import com.tianji.coupon.query.CouponQuery;
import com.tianji.coupon.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 优惠券的规则信息 前端控制器
 * </p>
 *
 * @author 陈佳
 * @since 2024-08-29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/coupons")
@Api(tags = "优惠券相关接口")
public class CouponController {
    private final CouponService couponService;

    @ApiOperation("新增优惠券接口")
    @PostMapping
    public Result saveCoupon(@RequestBody @Valid CouponFormDTO dto){
        couponService.saveCoupon(dto);
        return Result.success("新增优惠卷成功");
    }

    @ApiOperation("分页查询优惠券接口")
    @GetMapping("/page")
    public Result queryCouponByPage(CouponQuery query){
        return couponService.queryCouponByPage(query);
    }

    @ApiOperation("发放优惠券接口")
    @PutMapping("/issue")
    public void beginIssue(@RequestBody @Valid CouponIssueFormDTO dto) {
        couponService.beginIssue(dto);
    }

    @ApiOperation("修改优惠券接口")
    @PutMapping("/{id}")
    public Result updateCoupon(@RequestBody @Valid CouponFormDTO dto){
        couponService.updateCoupon(dto);
        return Result.success("修改优惠卷成功");
    }

    @ApiOperation("删除优惠券接口")
    @DeleteMapping("/{id}")
    public Result deleteCoupon(@PathVariable Long id){
        couponService.deleteCoupon(id);
        return Result.success("删除优惠卷成功");
    }



    @ApiOperation("查询优惠券详情")
    @GetMapping("/{id}")
    public Result CouponByid(@PathVariable Long id){
        return couponService.CouponByid(id);
    }

    @ApiOperation("暂停优惠券的发放")
    @PutMapping("/{id}/pause")
    public Result pauseCoupon(@PathVariable Long id){
        couponService.pauseCoupon(id);
        return Result.success("暂停优惠券的发放成功");
    }

}

